#include "common.h"
#include "hello.h"

void staticTest()
{
    std::cout<<"staticTest"<<std::endl;
    hello obj;
    obj.say();
    test();
}

void dynamicTest()
{
    std::cout<<"dynamicTest"<<std::endl;
    void *_soHandle = dlopen("../hello/output/so/Hello.so", RTLD_NOW);
    if(!_soHandle)
    {
        std::cout<<"open so failed."<<std::endl;
        return;
    }
    void (*test)();
    if(!(test = (void (*)())dlsym(_soHandle, "test")))
    {
        std::cout<<"find test error.";
    }

    test();

    dlclose(_soHandle);
}

void dynamicTestClass()
{
    std::cout<<"dynamicTestClass"<<std::endl;
    void *_soHandle = dlopen("../hello/output/so/Hello.so", RTLD_NOW);
    if(!_soHandle)
    {
        std::cout<<"open so failed."<<std::endl;
        return;
    }
    hello *(*create)();
    if(!(create = (hello* (*)())dlsym(_soHandle, "create")))
    {
        std::cout<<"find create error.";
    }
    void (*destory)(hello *p);
    if(!(destory = (void (*)(hello *p))dlsym(_soHandle, "destory")))
    {
        std::cout<<"find create error.";
    }

    hello *p = create();
    p->say();
    destory(p);
    dlclose(_soHandle);
}

int main()
{
    staticTest();
    dynamicTest();
    dynamicTestClass();
    std::cout<<"I am Main."<<std::endl;

    return 0;
}
